﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CRM.Business
{
    public class Product
    {
        /// <summary>
        /// 添加一条产品信息
        /// </summary>
        /// <param name="goodMember"></param>
        /// <returns></returns>
        public static bool AddProduct(DataLayer.Product products)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    db.Product.AddObject(products);
                    int changes = db.SaveChanges();
                    if (changes > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch (Exception)
            {

                return false;
            }
        }
        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="goodMember"></param>
        /// <returns></returns>
        public static bool ModifyProduct(DataLayer.Product products)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var member = db.Product.Single(p => p.productID == products.productID);
                    if (member != null)
                    {
                        member.model = products.model;
                        member.innerDiameter = products.externalDiameter;
                        member.height = products.height;
                        member.limitSpeed = products.limitSpeed;
                        member.referenceSpeed = products.referenceSpeed;
                        member.brand = products.brand;
                        member.image = products.image;                        
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }

            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 首页获得产品图片展示
        /// </summary>
        /// <returns></returns>
        public static IList<DataLayer.Product> ProductsShowByBrand(out int totleCount, int pageNum, int pageSize, string brand)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = from products in db.Product
                                 orderby products.brand descending
                                 where products.brand==brand
                                 select products;
                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        /// <summary>
        /// 首页获得产品图片展示
        /// </summary>
        /// <returns></returns>
        public static IList<DataLayer.Product> ProductsShow(out int totleCount, int pageNum, int pageSize, string type)
        {
            try
            {
                using (var db=new DataLayer.CRMDateEntities())
                {
                    var result = from products in db.Product
                                 orderby products.brand descending
                                 where products.type==type
                                 select products;
                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        public static IList<DataLayer.Product> ProductsShow(out int totleCount, int pageNum, int pageSize)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = from products in db.Product
                                 orderby products.brand descending                                 
                                 select products;
                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        public static IList<DataLayer.Product> ProductsShow()
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = from products in db.Product
                                 orderby products.brand descending
                                 select products;
                    return result.ToList();
                }
            }
            catch (Exception)
            {                
                return null;
            }
        }
        /// <summary>
        /// 返回产品信息列表
        /// </summary>
        /// <returns></returns>
        public static IList<CRM.Model.ProductSearchResult> PageListProducts(out int totleCount, int pageNum, int pageSize)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var result = from products in db.Product
                                 orderby products.productID descending
                                 select new CRM.Model.ProductSearchResult
                                 {
                                     ProductId=products.productID,
                                     Model = products.model,
                                     Type = products.type,
                                     Brand = products.brand,
                                     InnerDiameter = products.innerDiameter,
                                     ExternalDiameter = products.externalDiameter,
                                     Height = products.height,
                                     LimitSpeed = products.limitSpeed,
                                     ReferenceSpeed = products.referenceSpeed,
                                 };
                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        public static IList<CRM.Model.ProductSearchResult> PageListProducts(out int totleCount, int pageNum, int pageSize, string type, string content)
        {
            try
            {
                using (var db = new CRM.DataLayer.CRMDateEntities())
                {
                    var result = from products in db.Product
                                 orderby products.productID descending
                                 //where products.innerDiameter==Convert.ToInt32(content)
                                 select new CRM.Model.ProductSearchResult
                                 {
                                     ProductId = products.productID,
                                     Model = products.model,
                                     Type = products.type,
                                     Brand = products.brand,
                                     InnerDiameter = products.innerDiameter,
                                     ExternalDiameter = products.externalDiameter,
                                     Height = products.height,
                                     LimitSpeed = products.limitSpeed,
                                     ReferenceSpeed = products.referenceSpeed,
                                 };
                    if (type == "产品型号")
                    {
                        result = result.Where(p => p.Model.Contains(content));
                    }
                    else if (type == "产品内径")
                    {
                        result = result.Where(p => p.InnerDiameter==Convert.ToInt32(content));
                    }
                    else if (type == "产品外径")
                    {
                        result = result.Where(p => p.ExternalDiameter == Convert.ToInt32(content));
                    }
                    else if (type == "产品高度")
                    {
                        result = result.Where(p => p.Height == Convert.ToInt32(content));
                    }
                    //获取总记录数目
                    totleCount = result.ToArray().Length;
                    //获取请求页的最大索引
                    int max = (result.ToArray().Length <= pageNum * pageSize) ? result.ToArray().Length : pageNum * pageSize;
                    if (max < (pageNum - 1) * pageSize)
                    {
                        //请求页的最大索引比它上一页的最大索引还小：矛盾
                        return null;
                    }
                    else if (max < pageSize * pageNum)
                    {
                        //请求页是左后一页
                        return result.Skip((pageNum - 1) * pageSize).ToList();
                    }
                    else
                    {
                        //请求页正常返回
                        return result.Skip((pageNum - 1) * pageSize).Take(pageSize).ToList();
                    }
                }
            }
            catch (Exception)
            {
                totleCount = 0;
                return null;
            }
        }
        /// <summary>
        /// 获取一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DataLayer.Product GetNewById(int id)
        {
            var db = new CRM.DataLayer.CRMDateEntities();
            var result = db.Product.SingleOrDefault(p => p.productID == id);

            if (result != null)
            {
                return result;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 删除个产品记录
        /// </summary>
        /// <param name="news"></param>
        /// <returns></returns>
        public static bool DeleteProduct(DataLayer.Product products)
        {
            try
            {
                using (var db = new DataLayer.CRMDateEntities())
                {
                    var result = db.Product.SingleOrDefault(p => p.productID == products.productID);
                    if (result != null)
                    {
                        db.Product.DeleteObject(result);
                        int changes = db.SaveChanges();
                        if (changes > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch
            {
                return false;
            }


        }
    }
}
